package fr.utbm.tx52.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {
	
	private static String url = "localhost";
	private static String dbName = "to";
	private static String username = "to52";
	private static String password = "to52";
	
	private static boolean isInitiated = false;
	private static boolean isConnected = false;
	
	private static Connection connection = null;
	private static Statement instruction = null;
	
	public static void init() throws ClassNotFoundException {
		if (!isInitiated) {
			Class.forName("com.mysql.jdbc.Driver");
			isInitiated = true;
		}
	}
	
	public static void connect() throws SQLException {
		if (!isConnected) {
			connection = DriverManager.getConnection(
					"jdbc:mysql://"+url+"/"+dbName,
					username,password);
			isConnected = true;
			instruction = connection.createStatement();
		}
	}
	
	public static void close() throws SQLException {
		if (!connection.isClosed()) {
			instruction.close();
			connection.close();
		}
		isConnected = false;
	}
	
	public static Statement getInsctruction() {
		return instruction;
	}
}
